// axios 封装
import { message } from 'antd'
import axios from 'axios'
import { isAuth, getToken } from './token'
const http = axios.create({
  baseURL: 'http://geek.itheima.net/v1_0',
  timeout: 5000
})
// Add a request interceptor
http.interceptors.request.use(
  function (config) {
    if (isAuth()) {
      config.headers.Authorization = `Bearer ${getToken()}`
    }
    // Do something before request is sent
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

// Add a response interceptor
http.interceptors.response.use(
  function (response) {
    // Do something with response data
    return response.data
  },
  function (error) {
    message.error(error.response.data.message)
    // Do something with response error
    return Promise.reject(error)
  }
)

export { http }
